package com.ele.client.editor;

import com.ele.client.Presenter;
import com.ele.client.rpc.ApplicationServiceAsync;
import com.ele.model.Answer;
import com.ele.model.Question;
import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.TextArea;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

import java.util.ArrayList;
import java.util.List;

public class QuestionPresenter implements Presenter {

    public interface Display {
        Widget asWidget();

        TextArea getQuestionTextArea();

        Button getAddAnswerButton();

        Button getDeleteButton();

        HTMLPanel getAnswersPanel();

        Button getGenerateAnswerButton();

        TextBox getImgUrlTextBox();
    }

    private final ApplicationServiceAsync rpcService;
    private final HandlerManager eventBus;
    private final Display display;
    private final SectionPresenter parentPresenter;

    private int answerLabelCounter = 1;
    private Question value;
    private List<AnswerPresenter> presenters = new ArrayList<AnswerPresenter>();

    public QuestionPresenter(ApplicationServiceAsync rpcService, HandlerManager eventBus, Display display, SectionPresenter parentPresenter) {
        this.rpcService = rpcService;
        this.eventBus = eventBus;
        this.display = display;
        this.parentPresenter = parentPresenter;

        bind();
    }

    public void bind() {
        display.getAddAnswerButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                addAnswerControl(createAnswer(""));
                presenters.get(presenters.size()-1).setFocus(true);
            }
        });

        display.getGenerateAnswerButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                presenters.clear();
                display.getAnswersPanel().clear();

                String question = display.getQuestionTextArea().getValue();
                if (question.startsWith("Do you")) {
                    String positiveAnswer = question.replaceFirst("Do you", "Yes, I").replaceFirst("\\?$", "")
                            .replaceAll(" any ", " some ").replaceAll(" anything ", " something ");
                    addAnswerControl(createAnswer(positiveAnswer));
                    String negativeAnswer = question.replaceFirst("Do you", "No, I don't").replaceFirst("\\?$", "");
                    addAnswerControl(createAnswer(negativeAnswer));
                } else if (question.startsWith("Did you")) {
                    String positiveAnswer = question.replaceFirst("Did you", "Yes, I").replaceFirst("\\?$", "")
                            .replaceAll(" any ", " some ").replaceAll(" anything ", " something ");
                    addAnswerControl(createAnswer(positiveAnswer));
                    String negativeAnswer = question.replaceFirst("Did you", "No, I didn't").replaceFirst("\\?$", "");
                    addAnswerControl(createAnswer(negativeAnswer));
                } else if (question.startsWith("Are you")) {
                    String positiveAnswer = question.replaceFirst("Are you", "Yes, I'm").replaceFirst("\\?$", "")
                            .replaceAll(" any ", " some ").replaceAll(" anything ", " something ");
                    addAnswerControl(createAnswer(positiveAnswer));
                    String negativeAnswer = question.replaceFirst("Are you", "No, I'm not").replaceFirst("\\?$", "");
                    addAnswerControl(createAnswer(negativeAnswer));
                } else if (question.startsWith("Will there")) {
                    String positiveAnswer = question.replaceFirst("Will there", "Yes, there'll").replaceFirst("\\?$", "")
                            .replaceAll(" any ", " some ").replaceAll(" anything ", " something ");
                    addAnswerControl(createAnswer(positiveAnswer));
                    String negativeAnswer = question.replaceFirst("Will there", "No, there won't").replaceFirst("\\?$", "");
                    addAnswerControl(createAnswer(negativeAnswer));
                } else if (question.startsWith("Had you")) {
                    String positiveAnswer = question.replaceFirst("Had you", "Yes, I had").replaceFirst("\\?$", "")
                            .replaceAll(" any ", " some ").replaceAll(" anything ", " something ").replaceAll(" your ", " my ");
                    addAnswerControl(createAnswer(positiveAnswer));
                    String negativeAnswer = question.replaceFirst("Had you", "No, I hadn't").replaceFirst("\\?$", "").replaceAll(" your ", " my ");
                    addAnswerControl(createAnswer(negativeAnswer));
                } else if (question.startsWith("Must you")) {
                    String positiveAnswer = question.replaceFirst("Must you", "Yes, I must").replaceFirst("\\?$", "")
                            .replaceAll(" any ", " some ").replaceAll(" anything ", " something ").replaceAll(" you ", " I ").replaceAll(" your ", " my ");
                    addAnswerControl(createAnswer(positiveAnswer));
                    String negativeAnswer = question.replaceFirst("Must you", "No, I don't have").replaceFirst("\\?$", "")
                            .replaceAll(" you ", " I ").replaceAll(" your ", " my ");
                    addAnswerControl(createAnswer(negativeAnswer));
                } else if (question.startsWith("If you")) {
                    String positiveAnswer = question.replaceFirst("If you", "Yes, If I").replaceFirst(" do you", ", I").replaceFirst("\\?$", "")
                            .replaceAll(" you ", " I ").replaceAll(" your ", " my ");
                    addAnswerControl(createAnswer(positiveAnswer));
                    String negativeAnswer = question.replaceFirst("If you", "No, if I").replaceFirst(" do you", ", I don't").replaceFirst("\\?$", "")
                            .replaceAll(" you ", " I ").replaceAll(" your ", " my ");
                    addAnswerControl(createAnswer(negativeAnswer));
                } else if (question.startsWith("What's")) {
                    String answer = question.replaceFirst("What's ", "").replaceFirst("\\?$", " is ");
                    String firstLetter = String.valueOf(answer.charAt(0));
                    answer = answer.replaceFirst(firstLetter, firstLetter.toUpperCase());
                    addAnswerControl(createAnswer(answer));
                    presenters.get(presenters.size()-1).setFocus(true);
                }
            }
        });

        display.getDeleteButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                parentPresenter.remove(QuestionPresenter.this);
            }
        });
    }

    private void addAnswerControl(Answer answer) {
        AnswerView answerView = new AnswerView();
        AnswerPresenter answerPresenter = new AnswerPresenter(rpcService, eventBus, answerView, this);
        answerPresenter.setValue(answer);
//        answerPresenter.getDisplay().getLabel().setText("Answer #" + answerLabelCounter++);
        presenters.add(answerPresenter);

        display.getAnswersPanel().add(answerView);
    }

    public void go(final HasWidgets container) {
        bind();
        container.clear();
        container.add(display.asWidget());
    }

    public void setValue(Question question) {
        value = question;

        display.getQuestionTextArea().setValue(question.getText());
        display.getImgUrlTextBox().setValue(question.getImgUrl());

        presenters.clear();
        display.getAnswersPanel().clear();

        List<Answer> answers = question.getAnswers();
        if (answers == null || answers.isEmpty()) {
            addAnswerControl(createAnswer(""));
        } else {
            for (Answer answer : answers) {
                addAnswerControl(answer);
            }
        }
    }

    private Answer createAnswer(String text) {
        Answer answer = new Answer();
        answer.setText(text);
        return answer;
    }

    public Display getDisplay() {
        return display;
    }

    public Question getValue() {
        value.setText(display.getQuestionTextArea().getValue());
        value.setImgUrl(display.getImgUrlTextBox().getValue());

        List<Answer> answers = new ArrayList<Answer>();
        for (AnswerPresenter presenter : presenters) {
            Answer answer = presenter.getValue();
            answers.add(answer);
        }
        value.setAnswers(answers);

        return value;
    }

    public void remove(AnswerPresenter presenter) {
        presenter.getDisplay().asWidget().removeFromParent();
        presenters.remove(presenter);
    }

    public void setFocus(boolean focused) {
        display.getQuestionTextArea().setFocus(focused);
    }
}
